import logging.handlers
import time

from config import LOG_FORMAT, DATE_FORMAT, ERROR_FORMAT


def get_logger():
    logger = logging.getLogger('detector_logger')
    logger.setLevel(logging.DEBUG)
    logger.handlers = []

    # 在info日志文件名中添加检测的日期
    info_filename = 'info-' + time.strftime('%Y-%m-%d', time.localtime(time.time())) + '.log'
    info_handler = logging.handlers.TimedRotatingFileHandler(filename='logging/' + info_filename, when='midnight',
                                                             interval=1, backupCount=7, encoding='utf-8')
    info_handler.setLevel(logging.INFO)
    info_handler.setFormatter(logging.Formatter(LOG_FORMAT))

    danger_handler = logging.FileHandler('logging/danger.log')
    danger_handler.setLevel(logging.CRITICAL)
    danger_handler.setFormatter(logging.Formatter(LOG_FORMAT))

    error_handler = logging.FileHandler('logging/error.log')
    error_handler.setLevel(logging.ERROR)
    error_handler.setFormatter(logging.Formatter(ERROR_FORMAT))

    logger.addHandler(info_handler)
    logger.addHandler(danger_handler)
    logger.addHandler(error_handler)

    return logger

    # logger.debug('debug message')
    # logger.info('info message')
    # logger.warning('warning message')
    #
    # logger.error('error message')
    # logger.critical('critical message')
